from flask import request
from flask_restful import Resource, reqparse
from exts import api, db
from flask_docs import ApiDoc
from services.item_service import *
from services.inventory_service import *
from utils.auth import auth_service
from flask import jsonify
from models.user import RoleType

class Item_api(Resource):
    @auth_service().require_role(RoleType.BUSINESS)
    @auth_service().verify_token
    def post(self):
        try:
            post_json = request.get_json()
            user_id = auth_service().get_user_id_from_token()
            if post_json:
                item_id = item_service().create_item(post_json, user_id)
                return {
                    "message": "创建成功",
                    "item": item_id,
                    "code": 200  # 添加 code 信息
                }
            else:
                return jsonify({"message": "参数错误", "code": 400}), 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.BUSINESS)
    @auth_service().verify_token
    def get(self, id=None):
        try:
            if id:
                return {
                    "message": "查询成功",
                    "item": item_service().get_item_by_id(id),
                    "code": 200  # 添加 code 信息
                }
            else:
                if item_service().get_item_all() == []:
                    return {"message": "查询成功", "items": [], "code": 200}
                return {"message": "查询成功", "items": item_service().get_item_all(), "code": 200}
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.BUSINESS)
    @auth_service().verify_token
    def put(self):
        try:
            put_json = request.get_json()
            if put_json:
                item_info = item_service().update_item(put_json)
                return {
                    "message": "更新成功",
                    "item": item_info,
                    "code": 200  # 添加 code 信息
                }
            else:
                return jsonify({"message": "参数错误", "code": 400}), 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.BUSINESS)
    @auth_service().verify_token
    def delete(self):
        try:
            delete_json = request.get_json()
            if delete_json:
                item_info = item_service().delete_item(delete_json)
                return {
                    "message": "删除成功",
                    "item": item_info,
                    "code": 200  # 添加 code 信息
                }
            else:
                return jsonify({"message": "参数错误", "code": 400}), 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()


class inventory_api(Resource):
    @auth_service().require_role(RoleType.BUSINESS)
    @auth_service().verify_token
    def post(self):
        try:
            post_json = request.get_json()
            user_id = auth_service().get_user_id_from_token()
            if post_json:
                inventory_info = inventory_service().create_inventory(post_json, user_id)
                return {
                    "message": "库存记录创建成功",
                    "inventory": inventory_info,
                    "code": 200  # 添加 code 信息
                }
            else:
                return jsonify({"message": "参数错误", "code": 400}), 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role([RoleType.BUSINESS,RoleType.AUDIT])
    @auth_service().verify_token
    def get(self, id=None):
        try:
            user_id = auth_service().get_user_id_from_token()
            user_info = auth_service().get_user_info_from_token()
            if id:
                inventory_info = inventory_service().get_inventory(id, user_id,user_info)
                return {
                    "message": "查询成功",
                    "inventory": inventory_info,
                    "code": 200  # 添加 code 信息
                }
            else:
                inventories = inventory_service().get_all_inventories(user_id,user_info)
                if not inventories:
                    return {"message": "查询成功，无库存信息", "inventories": [], "code": 200}
                return {"message": "查询成功", "inventories": inventories, "code": 200}
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.BUSINESS)
    @auth_service().verify_token
    def put(self):
        try:
            put_json = request.get_json()
            user_id = auth_service().get_user_id_from_token()
            print("put",put_json)

            if put_json:
                inventory_info = inventory_service().update_inventory(put_json, user_id)
                return {
                    "message": "库存记录更新成功",
                    "inventory": inventory_info,
                    "code": 200  # 添加 code 信息
                }
            else:
                return jsonify({"message": "参数错误", "code": 400}), 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.BUSINESS)
    @auth_service().verify_token
    def delete(self):
        try:
            delete_json = request.get_json()
            user_id = auth_service().get_user_id_from_token()
            if delete_json:
                inventory_info = inventory_service().delete_inventory(delete_json, user_id)
                return {
                    "message": "库存记录删除成功",
                    "inventory": inventory_info,
                    "code": 200  # 添加 code 信息
                }
            else:
                return jsonify({"message": "参数错误", "code": 400}), 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.AUDIT)
    @auth_service().verify_token
    def patch(self, id):
        try:
            patch_json = request.get_json()
            status = patch_json.get("status")
            comment = patch_json.get("comment", "")
            auditor_id = auth_service().get_user_id_from_token()
            
            inventory_info = inventory_service().approve_inventory(
                inventory_id=id,
                status=status,
                comment=comment,
                auditor_id=auditor_id
            )
            return {
                "message": "库存审批成功",
                "inventory": inventory_info,
                "code": 200
            }
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()


api.add_resource(Item_api, "/api/Item_api", "/api/Item_api/<int:id>")
api.add_resource(inventory_api, "/api/inventory", "/api/inventory/<int:id>")